import { createStore, applyMiddleware } from 'redux'
import thunkMiddleware from 'redux-thunk'
// 引入 AsyncStorage 作为存储容器
import { AsyncStorage } from 'react-native'
// 引入 redux-persist
import { persistStore, persistCombineReducers } from 'redux-persist'

import rootReducer from './reducers/index'

// 持久化存储配置
const config = {
  key: 'root',
  storage: AsyncStorage
}

const persistReducers = persistCombineReducers(config, rootReducer)

const createStoreWithMiddleware = applyMiddleware(thunkMiddleware)(createStore)

export default function configureStore (initialState) {
  const store = createStoreWithMiddleware(persistReducers, initialState)
  const persistor = persistStore(store)
  return { persistor, store }
}
